/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.etcitem.action;

import java.lang.reflect.Constructor;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.lineage.server.IdFactory;
import com.lineage.server.PacketCreate;
import com.lineage.server.datatables.FurnitureItemTable;
import com.lineage.server.datatables.FurnitureSpawnTable;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.L1HouseLocation;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1World;
import com.lineage.server.model.Instance.L1FurnitureInstance;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.ItemExecutor;
import com.lineage.server.templates.L1FurnitureItem;
import com.lineage.server.templates.L1Npc;

/**
 * Class <code>Furniture</code> 家具 (41383 - 41400/49065 - 49076).
 * 
 * @author jrwz
 * @version 2012-6-23上午09:34:52
 * @see com.lineage.server.model.item.etcitem.action
 * @since JDK1.7
 */
public final class Furniture extends ItemExecutor {

    /** 提示信息. */
    private static final Logger LOG = Logger.getLogger(Furniture.class
            .getName());

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static ItemExecutor get() {
        return new Furniture();
    }

    /** 家具 (41383 - 41400). */
    private Furniture() {
    }

    @Override
    public void execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        final int itemObjid = useItem.getId();
        final int itemId = useItem.getItemId();
        this.useFurnitureItem(pc, itemId, itemObjid);
    }

    /**
     * 使用家具.
     * 
     * @param pc
     *            - 对象
     * @param itemId
     *            - 道具编号
     * @param itemObjectId
     *            - 道具的OBJID
     */
    private void useFurnitureItem(final L1PcInstance pc, final int itemId,
            final int itemObjectId) {
        final L1FurnitureItem furnitureItem = FurnitureItemTable.getInstance()
                .getTemplate((itemId));

        if (furnitureItem == null) {
            PacketCreate.get().sendSinglePackets(pc, "没有任何事情发生");
            return;
        }

        // 不能使用家具的地图
        if (!L1HouseLocation.isInHouse(pc.getX(), pc.getY(), pc.getMapId())) {
            PacketCreate.get().sendSinglePackets(pc, "你无法在这个地方使用");
            return;
        }

        boolean isAppear = true;
        L1FurnitureInstance furniture = null;
        for (final L1Object l1object : L1World.getInstance().getObject()) {
            if (l1object instanceof L1FurnitureInstance) {
                furniture = (L1FurnitureInstance) l1object;
                if (furniture.getItemObjId() == itemObjectId) { // 家具已经召出
                    isAppear = false;
                    break;
                }
            }
        }

        if (isAppear) {
            if ((pc.getHeading() != 0) && (pc.getHeading() != 2)) {
                return;
            }
            final int npcId = furnitureItem.getFurnitureNpcId();
            try {
                final L1Npc l1npc = NpcTable.getInstance().getTemplate(npcId);
                if (l1npc != null) {
                    try {
                        final String s = l1npc.getImpl();
                        final Constructor<?> constructor = Class.forName(
                                "com.lineage.server.model.Instance." + s
                                        + "Instance").getConstructors()[0];
                        final Object aobj[] = { l1npc };
                        furniture = (L1FurnitureInstance) constructor
                                .newInstance(aobj);
                        furniture.setId(IdFactory.getInstance().nextId());
                        furniture.setMap(pc.getMapId());
                        if (pc.getHeading() == 0) {
                            furniture.setX(pc.getX());
                            furniture.setY(pc.getY() - 1);
                        } else if (pc.getHeading() == 2) {
                            furniture.setX(pc.getX() + 1);
                            furniture.setY(pc.getY());
                        }
                        furniture.setHomeX(furniture.getX());
                        furniture.setHomeY(furniture.getY());
                        furniture.setHeading(0);
                        furniture.setItemObjId(itemObjectId);

                        L1World.getInstance().storeObject(furniture);
                        L1World.getInstance().addVisibleObject(furniture);
                        FurnitureSpawnTable.getInstance().insertFurniture(
                                furniture);
                    } catch (final Exception e) {
                        LOG.log(Level.SEVERE, e.getLocalizedMessage(), e);
                    }
                }
            } catch (final Exception exception) {
                LOG.log(Level.SEVERE, exception.getLocalizedMessage(),
                        exception);
            }
        } else {
            furniture.deleteMe();
            FurnitureSpawnTable.getInstance().deleteFurniture(furniture);
        }
    }
}
